package com.example.demo.modules.controller;

import com.example.demo.modules.service.ShopService;
import com.example.demo.util.Validator;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * Created by asus on 2018/3/20.
 */
@Controller
@RequestMapping("/rm")
public class ShopController {
    @Autowired
    private ShopService shopService;

    /**
     *
     * type   0.商户名称  1.商户税号
     * value  商户名称 或则 商户税号
     * @return
     */
    @RequestMapping(value = "/v1",produces = "application/json;charset=UTF-8",method = RequestMethod.POST)
    @ResponseBody
    public String creditRisk( HttpServletRequest request) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            IOUtils.copy(request.getInputStream(),bos);
        } catch (IOException e) {
            e.printStackTrace();
        }


        System.out.println("merchant-------------------------------->");
        System.out.println("data:"+bos.toString());
        Validator validator = new Validator();
        JSONObject object = validator.validate(bos.toString());
        String result = null;
        if("0000".equals(object.get("errorCode"))) {
            int type = 0;
            String value = null;
            try {
                type = object.getInt("type");
                value = object.getString("value");
                result = shopService.creditRisk(type, value);
            } catch (Exception e) {
                e.printStackTrace();
                object.put("errorCode","1003");
                result = object.toString();
            }
        }else {
            result = object.toString();
        }
        System.out.println(result);
        return result;
    }
}
